﻿using QuestionnaireInvestigation.Data.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QuestionnaireInvestigation.Data.DTOS;

namespace QuestionnaireInvestigation.Data.Service
{
    public class QuestionnaireService : IDisposable
    {
        private QuestionnaireInvestigationDBEntities data;
        public QuestionnaireService()
        {
            data = new QuestionnaireInvestigationDBEntities();
        }

        public void Dispose()
        {
            data.Dispose();
        }

        public Questionnaire CreateQuestionnaire(string title)
        {
            Questionnaire questionnaire = new Questionnaire
            {
                Title = title,
                CreateTime = DateTime.Now
            };
            data.Questionnaires.Add(questionnaire);
            data.SaveChanges();
            return questionnaire;
        }

        public void AddOptionsToChoiceQuestion(RadioDTO radio)
        {
            ChoiceQuestion question = new ChoiceQuestion
            {
                Qid = radio.Qid,
                Title = radio.RadioTitle,
            };
            if (radio.Type == "radio")
            {
                question.Type = true;
            }
            else
            {
                question.Type = false;
            }
            data.ChoiceQuestions.Add(question);
            data.SaveChanges();
            foreach (var option in radio.RadioOptions)
            {
                Option temp = new Option
                {
                    QuestionId = question.QuestionId,
                    OptionContent = option,
                };
                data.Options.Add(temp);
            }
            data.SaveChanges();
        }

        public Questionnaire GetQuestionnaireById(int id)
        {
            return data.Questionnaires.SingleOrDefault(q => q.Qid == id);
        }

        public void AddCompletionToQuestionnaire(CompletionDTO completion)
        {
            Completion com = new Completion
            {
                Qid = completion.Qid,
                Title = completion.Title
            };
            data.Completions.Add(com);
            data.SaveChanges();
        }

        public List<Questionnaire> GetAllQuestionnaire()
        {
            return data.Questionnaires.ToList();
        }

        public int CreateAnswer(string ip,int qId)
        {
            Answer answer = new Answer
            {
                CreateTime = DateTime.Now,
                IpAddress = ip,
                Qid =qId
            };
            data.Answers.Add(answer);
            data.SaveChanges();
            return answer.AnswerId;
        }

        public void AddCompletionAnswer(int answerId, string answerContent, int completionId)
        {
            var com = data.Completions.SingleOrDefault(c => c.CompletionId == completionId);
            CompletionAnswerOption answer = new CompletionAnswerOption
            {
                CompletionId = com.CompletionId,
                AnswerContent = answerContent,
                AnswerId = answerId
            };
            data.CompletionAnswerOptions.Add(answer);
            data.SaveChanges();
        }

        public void AddRadioAnswer(int answerId, int questionId, string answerOption)
        {
            var question = data.ChoiceQuestions.SingleOrDefault(c => c.QuestionId == questionId);
            ChoiceAnswerOption answer = new ChoiceAnswerOption
            {
                QuestionId = question.QuestionId,
                AnswerId = answerId
            };
            var option = question.Options.SingleOrDefault(o => o.OptionContent == answerOption);
            answer.OptionId = option.OptionId;
            data.ChoiceAnswerOptions.Add(answer);
            data.SaveChanges(); 
        }

        public void AddMultiselectAnswer(int answerId, int questionId, List<string> answerOptions)
        {
            var question = data.ChoiceQuestions.SingleOrDefault(c => c.QuestionId == questionId);
            foreach(var optionContent in answerOptions)
            {
                ChoiceAnswerOption answer = new ChoiceAnswerOption
                {
                    QuestionId = question.QuestionId,
                    AnswerId = answerId
                };
                var option = question.Options.Where(o => o.OptionContent == optionContent).First();
                answer.OptionId = option.OptionId;
                data.ChoiceAnswerOptions.Add(answer);
            }
            data.SaveChanges();
        }

        public void DeleteQuestionnaireById(int id)
        {
            var questonnaire = data.Questionnaires.SingleOrDefault(q => q.Qid == id);
            List<Option> optionsTemp = new List<Option>();
            List<ChoiceQuestion> choiceQuestionTemp = new List<ChoiceQuestion>();
            List<Completion> completionTemp = new List<Completion>();
            if (questonnaire.Answers.Count > 0)
            {
                throw new Exception("问卷已被填写，请勿删除");
            }
            foreach(var choiceQuestion in questonnaire.ChoiceQuestions)
            {
                var question = data.ChoiceQuestions.SingleOrDefault(c => c.QuestionId == choiceQuestion.QuestionId);
                foreach(var option in question.Options)
                {
                    var opt = data.Options.SingleOrDefault(o => o.OptionId == option.OptionId);
                    optionsTemp.Add(opt);
                }
                choiceQuestionTemp.Add(choiceQuestion);
            }
            foreach(var com in questonnaire.Completions)
            {
                var completion = data.Completions.SingleOrDefault(c => c.CompletionId == com.CompletionId);
                completionTemp.Add(com);
            }
            data.Options.RemoveRange(optionsTemp);
            data.ChoiceQuestions.RemoveRange(choiceQuestionTemp);
            data.Completions.RemoveRange(completionTemp);
            data.Questionnaires.Remove(questonnaire);
            data.SaveChanges();
        }

        public List<Answer> GetAllAnswer()
        {
            return data.Answers.ToList();
        }

        public Answer GetAnswerById(int id)
        {
            return data.Answers.SingleOrDefault(a => a.AnswerId == id);
        }
    }
}
